<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <style type="text/css">
        .layui-fluid {
            padding: 15px !important;
        }

        .my-card {
            width: 600px;
            padding: 15px;
            background-color: #fff;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-container my-card">
        <form class="layui-form layui-form-pane" action="" lay-filter="userInfo">
            <input name="id" hidden>
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="username" lay-verify="title" disabled="disabled"
                           style="background:#eeeeee!important" autocomplete="off" placeholder="请输入账号"
                           class="layui-input layui-disabled ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">真实姓名</label>
                <div class="layui-input-block">
                    <input type="realName" name="realName" placeholder="请输入真实姓名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-block">
                    <input type="nickName" name="nickName" placeholder="请输入昵称" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-block">
                    <input type="tel" name="phone" placeholder="请输入手机号"
                           lay-verify="phone"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" placeholder="请输入邮箱"
                           lay-verify="email"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁止" lay-filter="switch"
                           checked="checked" disabled="disabled">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单选框</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="1" title="男">
                    <input type="radio" name="sex" value="2" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-btn-container" style="text-align: center">
                    <button type="submit" class="layui-btn" style="margin: 0" lay-submit="" lay-filter="submit">
                        保存
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/js/core.util.js}"></script>
<script>
    layui.use(['jquery', 'form'], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var basePath = CoreUtil.getContextPath();
        var $layuiStatus = $(".layui-container input[name=status]");
        CoreUtil.sendAjax(basePath + "/api/user/info", null, function (res) {
            if (res.data != null) {
                $(".layui-container input[name=id]").val(res.data.id);
                $(".layui-container input[name=username]").val(res.data.username);
                $(".layui-container input[name=realName]").val(res.data.realName);
                $(".layui-container input[name=nickName]").val(res.data.nickName);
                $(".layui-container input[name=email]").val(res.data.email);
                $(".layui-container input[name=phone]").val(res.data.phone);
                $(".layui-container input[name=sex][value='1']").attr("checked", res.data.sex === 1);
                $(".layui-container input[name=sex][value='2']").attr("checked", res.data.sex === 2);
                if (res.data.status === 1) {
                    $layuiStatus.attr('checked', 'checked');
                    $layuiStatus.attr('type', 'hidden').val(1);
                } else {
                    $layuiStatus.attr('type', 'hidden').removeAttr("checked").val(2);
                    var x = document.getElementsByClassName("layui-unselect layui-form-switch layui-form-onswitch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch");
                    var d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '禁用';
                }
                form.render(); //更新全部
            }
        }, "GET", false);

        //表单验证
        form.verify({
            email: function (value) {
                if (value != null && value !== "" && !(/^([a-zA-Z0-9_.\-])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value))) {
                    return '邮箱格式不正确';
                }
            },

            phone: function (value) {
                if (value != null && value !== "" && !(/^1[3456789]\d{9}$/.test(value))) {
                    return '请输入正确的手机号';
                }
            }
        });

        //监听指定开关
        form.on('switch(switch)', function () {
            $layuiStatus.attr('type', 'hidden').val(this.checked ? 1 : 2);
        });

        //监听提交
        form.on('submit(submit)', function (data) {
            CoreUtil.sendAjax(basePath + "/api/user/info", JSON.stringify(data.field), function (res) {
                layer.msg(res.msg);
            }, "PUT", false);
            return false;
        });
    })
</script>
</html>